package com.jn.algorithm.day025.question81;

/**
 * @author 江南大学1033190417
 * @date 2022/5/16 18:46
 */
public class Solution {
    public boolean search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (nums[mid] == target) {
                return true;
            }
            if (nums[l] == nums[mid] && nums[mid] == nums[r]) {
                l++;
                r--;
            } else {
                if (nums[mid] <= nums[r]) {//如果右边是有序的
                    if (nums[mid] <= target && target <= nums[r]) {
                        l = mid + 1;
                    } else {
                        r = mid - 1;
                    }
                } else {//左边有序
                    if (nums[mid] >= target && target >= nums[l]) {
                        r = mid - 1;
                    } else {
                        l = mid + 1;
                    }
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{2, 5, 6, 0, 0, 1, 2};
        int target = 3;
        System.out.println(solution.search(nums, target));
    }
}
